<?php
namespace app\index\controller;
use \app\index\controller\Base;
use \think\Db;
class Shegong extends Base
{
    public function index()
    {
		$shequ = Db::name("shequ")->where("pid",0)->select();
		$this->assign("shequ",$shequ);
		$t=Db::name("case_type")->select();
		$type=$this->menulist($t);
		$this->assign("type",$type);
        $seo=Db::name("seo")->where("class","shegong")->find();
        $this->assign("seo",$seo);
    	$this->assign('menu','6');
    	// $this->assign('_title','新闻列表');
        return $this->fetch();
    }

    //新闻详情页
    public function info(){
    	//新闻
        $news=Db::name('news')->where('id',$this->request->param('id'))->find();
        $this->assign('news',$news);
    	// $this->assign('menu','3');
    	// $this->assign('_title',"新闻详情");
        return $this->fetch();
    }
	
	 public function menulist($menu,$id=0,$level=0){
        
        static $menus = array();
        $size = count($menus)-1;
        foreach ($menu as $value) {
            if ($value['pid']==$id) {
                $value['level'] = $level+1;
                if($level == 0)
                {
                    $value['str'] = str_repeat('',$value['level']);
                    $menus[] = $value;
                }
                elseif($level == 2)
                {
                    $value['str'] = '&emsp;&emsp;&emsp;&emsp;'.'└ ';
                    $menus[$size]['list'][] = $value;
                }
                elseif($level == 3)
                {
                    $value['str'] = '&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;'.'└ ';
                    $menus[$size]['list'][] = $value;
                }
                else
                {
                    $value['str'] = '&emsp;&emsp;'.'└ ';
                    $menus[$size]['list'][] = $value;
                }
                
                $this->menulist($menu,$value['id'],$value['level']);
            }
        }
        return $menus;
    }
	
	public function ajax(){
		$id=Request()->param("id");
		if($id==""){
			return  '<option value="">请选择</option>';
		}else{
			
		
		$res = Db::name("shequ")->where("pid",$id)->select();
		$a='';
		foreach($res as $key =>$value){
			$a.='<option value="'.$value['id'].'">'.$value['name'].'</option>';
		}
		// var_dump($a);exit;
		return   $a;
		}
	}
	
	
	public function add(){
		$post=Request()->post();
		// return Request()->file("img");
		if(is_array($_FILES['img']['name'])){
			
			$imgPath = $_SERVER['DOCUMENT_ROOT'].config('view_replace_str')['__WEBUPLOAD__'].'/shegong/';
			$imgs = $this->uploadMulti('img',$imgPath);
			if(is_array($imgs)){
				$img="";
				foreach($imgs as $k=>$v){
					$img.=$v.",";
				}
			}
		}
		$data=[
			"name"=>$post['name'],
			"phone"=>$post['phone'],
			"area"=>$post['area'],
			"jie"=>$post['jie'],
			"dec"=>$post['dec'],
			"techang"=>$post['techang'],
			"img"=>$img,
			"create_time"=>time(),
		];
		// return $data;
		Db::name("shegong")->insert($data);
		return $this->success("申请成功");
	}
	
	function uploadMulti($name,$dir = ''){
		if($dir=='') $dir = $GLOBALS['config']['file_path'];
		if(!is_array($_FILES[$name]['name'])){
			$File = $this->uploadfile($ARG=array(
					'File'     => array('name'=>$_FILES[$name]['name'],'tmp_name' => $_FILES[$name]['tmp_name']),
					'Dir'=>$dir
			));
			$files = $File['newname'];
		}else{
			$files = array();
			foreach($_FILES[$name]['name'] as $k=>$v){
				if($_FILES[$name]['name'][$k]!=''){
					$File = $this->uploadfile(array(
							'File'=>array('name'=>$_FILES[$name]['name'][$k],'tmp_name'=>$_FILES[$name]['tmp_name'][$k]),
							'Dir'=>$dir
					));
					$files[] = $File['newname'];
				}
			}
		}
			
		return $files;
	}
	/**
	 * @desc 文件上传方法
	 * @param Array $ARG       上传文件的属性
	 */
	public function uploadfile($ARG=array(
	'File'  => array(),
	'Dir'   =>         '')){
		//默认目录
		$dir = "uploads/";
		//文件原始名称
		$oldname = $ARG['File']['name'];
		//文件类型
		$tmp = explode('.',$oldname);
		$filetype = $tmp[count($tmp)-1];
		//重命名(文件新名称)
		$newname = time().rand(1000,9999).".".$filetype;
		//上传目录处理
		if(!isset($ARG['Dir']) || $ARG['Dir']==''){
			$dir .= '';
		}else{
			$dir = $ARG['Dir'];
		}
		if(file_exists($dir)){
			$uploaddir = $dir;
		}
		else{
			$uploaddir = $this->setDir("./upload/");
		}
		//上传
		$uploadfile = $uploaddir.$newname;
		//echo $ARG['File']['tmp_name'];
		if(is_uploaded_file($ARG['File']['tmp_name'])){
			if(move_uploaded_file($ARG['File']['tmp_name'],$uploadfile)){
			
			}else{
			
			}
		}
		//返回数据，便于操作。
		$File  = array();
		$File['oldname']    =  $oldname;
		$File['newname']  =  $newname;
		$File['uploadfile']  =  $uploadfile;
		return $File;
	}
	/**
	 * @desc 创建目录
	 *
	 * @param String $dir
	 * @param default $mod
	 */
	public function setDir($dir,$mod=0777){
		//路径格式是否正确，以/结束。
		//echo $dir;
		//echo substr($dir,-1);
		if(substr($dir,-1) !='/'){
			$dir .= '/';
		}
		if(!file_exists($dir)){
			$dirs = explode('/',$dir);
			$path =$dirs[0];
			for($i=0;$i<count($dirs);$i++){
				$path .= '/'.$dirs[$i];
				try{
				if(!is_dir($path)){
					if(!mkdir($path,$mod)){
						$error = "不能创建上传目录：".$path;
						throw  new Exception($error);
					}
				  }
				}
				catch (Exception $e){
					echo $e->getMessage();
				}
			}
			if(realpath($path)){
				return $path;
			}
		}
	}
}
